package com.example.serviceheadlines.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.serviceheadlines.service.*;
import com.tangyuan.common.result.Result;
import com.tangyuan.common.result.ResultCodeEnum;
import com.tangyuan.dto.HeadlineArticlePublishDto;
import com.tangyuan.model.Headlines;
import com.tangyuan.model.headlines.HeadlinesCategoryQueryDto;
import com.tangyuan.model.headlines.HeadlinesContentQueryDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;

@RestController
@RequestMapping("/headlines")
@Slf4j
public class HeadlinesController {


    @Autowired
    private HeadlinesService headlinesService;


    /**
     * 浏览量+1 接口
     *
     * @return
     */
    @GetMapping("/browseHeadlines")
    public Result<ResultCodeEnum> browseHeadlines(@RequestParam(value = "id") Long id) {
        headlinesService.browseHeadlines(id);
        return Result.ok();
    }

    /**
     * 根据分类查询
     */
    @GetMapping("/getHeadlinesList")
    public Result<Page<Headlines>> getHeadlinesService(@RequestHeader(name = "userId", required = false) Long userId,
                                      @ModelAttribute HeadlinesCategoryQueryDto dto) {
        return Result.ok(headlinesService.getHeadlinesList(userId,dto));
    }

    /**
     * 查询百家列表
     */
    @GetMapping("/getHeadlinesConcernList")
    public Result<Page<Headlines>> getHeadlinesConcernList(@RequestHeader(name = "userId", required = false) Long userId,
                                                           @ModelAttribute HeadlinesContentQueryDto dto) {
        return Result.ok(headlinesService.searchHeadlines(userId,dto));
    }

    /**
     * 根据标题查询
     */
    @GetMapping("/searchHeadlines")
    public Result<Page<Headlines>> searchHeadlines(@RequestHeader(name = "userId",required = false) Long userId,
                                                                                                         @ModelAttribute HeadlinesContentQueryDto dto) {
        return Result.ok(headlinesService.searchHeadlines(userId,dto));
    }
    /**
     * 查询百家详情
     */
    @GetMapping("/getHeadlinesContent")
    public Result getHeadlinesContent(@RequestHeader(name = "userId", required = false) Long userId,
                                                 @RequestParam(value = "headlinesId") Long headlinesId){
        Headlines headlines = headlinesService.getHeadlinesById(headlinesId, userId);
        return headlines != null ? Result.ok(headlines) : Result.fail(ResultCodeEnum.NO_SEARCH_DATA);
    }


    @PostMapping("/headlinePublish")
    public Result headlinePublish(@RequestHeader(name = "Authorization",required = false) String token,
                                  @ModelAttribute @Valid HeadlineArticlePublishDto headlineArticlePublishDto){
        return headlinesService.headlinePublish(token,headlineArticlePublishDto);
    }

    @PostMapping("/headlineVideoPublish")
    public Result headlineVideoPublish(@RequestHeader(name = "Authorization",required = false) String token,
                                       @RequestParam("categoryId") Long categoryId,
                                       @RequestParam("title") String title,
                                       @RequestParam("area") String area,
                                       @RequestParam(value = "file",required = false) MultipartFile file,
                                       @RequestParam(value = "file1",required = false) MultipartFile file1,
                                       @RequestParam(value = "videoUrl",required = false) String videoUrl,
                                       @RequestParam(value = "videoFirstImageUrl",required = false) String videoFirstImageUrl){
        return headlinesService.headlineVideoPublish(token,categoryId,area,title,file,file1,videoUrl,videoFirstImageUrl);
    }

    @PostMapping("/deleteHeadlinesById")
    public Result deleteHeadlinesById(@RequestHeader(name = "Authorization",required = false) String token,
                                      @RequestParam("headlinesId") Long headlinesId){
        return headlinesService.deleteHeadlinesById(token,headlinesId);
    }
}

